<template>
	<view :style="sticky? 'position: sticky;top:0px;z-index: 999;':''" class="tabBlock" v-if="type.length > 0">
		<scroll-view show-scrollbar="false" scroll-x="true" scroll-with-animation :scroll-left="tabsScrollLeft"
			@scroll="scroll">
			<view class="tab" id="tab_list">
				<view v-for="(item, index) in type" :key="index"
					:class="['tab__item', {'tab__item--active': currentIndex === index}]"
					:style="{color: (currentIndex === index ? `${itemColor}`: '')}" id="tab_item"
					@click="select(item, index)">
					<view class="tab__item-title">
						{{item.title}}
					</view>
				</view>
			</view>
			<view class="tab__line"
				:style="{background: lineColor, width: lineStyle.width, transform: lineStyle.transform,transitionDuration: lineStyle.transitionDuration}">
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		props: {
			value: [Number, String],
			type: { // 传值
				type: Array,
				default: () => {
					return []
				}
			},
			itemColor: String, // tab主色调
			lineColor: String, // 下划线主色调
			lineAnimated: { // 是否展示下划线动画
				type: Boolean,
				default: true
			},
			sticky: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				currentIndex: 0,
				lineStyle: {},
				scrollLeft: 0,
				tabsScrollLeft: 0,
				duration: 0.3,
				tabList: undefined,
				tabItems: [], //每个item的宽度

				tempLineWidth: 0,
				tempLineLeft: 0
			}
		},
		watch: {
			type() {
				this.setTabList()
			},
			value() {
				this.currentIndex = this.value
				this.setTabList()
			}
		},
		mounted() {
			this.currentIndex = this.value
			this.setTabList()
			if (!this.lineAnimated) {
				this.duration = 0
			}
		},
		methods: {
			select(item, index) {
				this.$emit('input', index)
			},
			setTabList() {
				if (this.tabList) {
					this.$nextTick(() => {
						if (this.type.length > 0) {
							this.setLine()
							this.scrollIntoView()
						}
					})
				} else {
					this.getElementData(`#tab_list`, (data) => {
						this.tabList = data[0]
						this.getElementData(`#tab_item`, (subData) => {
							this.tabItems = subData

							this.$nextTick(() => {
								if (this.type.length > 0) {
									this.setLine()
									this.scrollIntoView()
								}
							})
						})
					})
				}
			},
			setLine() {
				this.tempLineWidth = this.tabItems[this.currentIndex].width / 2

				// lineLeft = el.width * (this.currentIndex + 0.5)  // 此种只能针对每个item长度一致的
				this.tempLineLeft = this.tabItems[this.currentIndex].width / 2 + (-this.tabItems[0].left) + this.tabItems[
					this.currentIndex].left
				this.lineStyle = {
					width: `${this.tempLineWidth}px`,
					transform: `translateX(${this.tempLineLeft}px) translateX(-50%)`,
					transitionDuration: `${this.duration}s`
				};
			},
			scrollIntoView() { // item滚动
				this.tempLineLeft = this.tabItems[this.currentIndex].width / 2 + (this.tabList.left) + this.tabItems[this
					.currentIndex].left - this.tabList.width / 2 - this.scrollLeft
				this.tabsScrollLeft = this.scrollLeft + this.tempLineLeft
			},
			getElementData(el, callback) {
				uni.createSelectorQuery().in(this).selectAll(el).boundingClientRect().exec((data) => {
					callback(data[0]);
				});
			},
			scroll(e) {
				this.scrollLeft = e.detail.scrollLeft;
			}
		}
	}
</script>

<style lang="scss">
	.tabBlock {
		position: relative;
		background: #fff;

		.tab {
			position: relative;
			display: flex;
			font-size: 28rpx;
			padding-bottom: 15rpx;
			white-space: nowrap;

			&__item {
				flex: 1;
				// width: 30%;
				text-align: center;
				line-height: 90rpx;
				color: $uni-text-color;

				&--active {
					color: $uni-color-primary;
				}

				&-title {
					margin: 0 40rpx;
				}
			}
		}

		.tab__line {
			display: block;
			height: 6rpx;
			position: absolute;
			bottom: 15rpx;
			left: 0;
			z-index: 1;
			border-radius: 3rpx;
			position: relative;
			background: $uni-color-primary;
		}
	}

	// 隐藏scroll-view的滚动条
	::-webkit-scrollbar {
		display: none;
		-webkit-appearance: none;
		width: 0;
		height: 0;
	}
</style>